//
//  SAPrintSku.m
//  Saas
//
//  Created by 杨荣 on 2020/6/8.
//  Copyright © 2020 bing. All rights reserved.
//

#import "SAPrintSku.h"
#import "SASalesOrderViewListModel.h"


@implementation SAPrintSku

#pragma mark - 尺码排序
- (NSArray <NSString *>*)getSortSize:(NSArray <NSString *>*)sieArray {
    
    if (sieArray.count == 0) {
        return nil;
    }
    
    NSMutableArray * initList = [[NSMutableArray alloc]init];
    NSMutableArray * mArray = [NSMutableArray arrayWithArray:sieArray];
    
    // 排版协议模板
    NSArray * initSize = @[@"4XS",@"XXXXS",@"3XS",@"XXXS",@"2XS",@"XXS",@"XS",@"S", @"M", @"L", @"XL", @"XXL",@"2XL",@"XXXL",@"3XL",@"XXXXL",@"4XL"];
    
    //从array数组中按照模板顺序取出值
    for (int i = 0; i < initSize.count; i++) {
        NSArray * tm = [mArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"SELF =[c] %@",initSize[i]]];
        if (tm.count != 0) {
            [initList addObject:tm.firstObject];
            [mArray removeObject:tm.firstObject];
        }else{
            //            [tempArray addObject:initSize[i]];
        }
    }
    
    NSMutableArray * numsArray = [NSMutableArray array];
    NSMutableArray * otherArray = [NSMutableArray array];
    for (int i = 0; i < mArray.count; i ++) {
        
        if ([self isPureInt:mArray[i]]) {
            
            [numsArray addObject:mArray[i]];
        }else{
            [otherArray addObject:mArray[i]];
        }
    }
    //纯数字按照大小排序
    if (numsArray.count != 0) {
        NSArray * sortedArray = [numsArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSString * str1 = obj1;
            NSString * str2 = obj2;
            return [str1 compare:str2 options:NSNumericSearch];
        }];
        [initList addObjectsFromArray:sortedArray];
    }
    //其他数字+字母按照 数字排序
    if (otherArray.count != 0) {
        NSArray * sortedArray = [otherArray sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            NSString * str1 = obj1;
            NSString * str2 = obj2;
            return [str1 compare:str2 options:NSNumericSearch];
        }];
        [initList addObjectsFromArray:sortedArray];
    }
    return initList;
}
- (BOOL)isPureInt:(NSString *)string{
    NSScanner* scan = [NSScanner scannerWithString:string];
    int val;
    return [scan scanInt:&val] && [scan isAtEnd];
}






//- (NSArray <SASalesOrderToPendSkuList *>*)getSkuArrayWithProductSkuArray:(NSArray <SASalesOrderViewListModel *>*)productSkuArray sizeArray:(NSArray *)sizeArray {
//
//    NSMutableArray <SASalesOrderToPendSkuList *>*array = [NSMutableArray array];
//
//    NSArray *productCodeArray = [productSkuArray valueForKeyPath:@"@distinctUnionOfObjects.productCode"];
//    for (NSString *productCode in productCodeArray) {
//        for (NSString *size in sizeArray) {
//            for (SASalesOrderViewListModel *product in productSkuArray) {
//                NSArray *filterArray = [product.toPendSkuAttrs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
//                   SASalesOrderToPendSkuList *model = evaluatedObject;
//                    return [model.sizeName isEqualToString:size];
//                }]];
//                if (filterArray.count > 0) {
//                    [array addObjectsFromArray:filterArray];
//                }
//            }
//        }
//    }
//    return array;
//}

- (NSArray <SASalesOrderViewListModel *>*)getProductSkuArrayBySizeWithProductSkuArray:(NSArray <SASalesOrderViewListModel *>*)productSkuArray sizeArray:(NSArray *)sizeArray {

    NSMutableArray <SASalesOrderViewListModel *>*array = [NSMutableArray array];

    for (SASalesOrderViewListModel *product in productSkuArray) {
        NSMutableArray *toPendSkuAttrs = [NSMutableArray array];
       for (NSString *size in sizeArray) {
           NSArray *filterArray = [product.toPendSkuAttrs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
              SASalesOrderToPendSkuList *model = evaluatedObject;
               return [model.sizeName isEqualToString:size];
           }]];
           if (filterArray.count > 0) {
               [toPendSkuAttrs addObjectsFromArray:filterArray];
           }
       }
        if (toPendSkuAttrs.count > 0) {
            [product.toPendSkuAttrs removeAllObjects];
            [product.toPendSkuAttrs addObjectsFromArray:toPendSkuAttrs];
            [array addObject:product];
        }
    }
    return array;
}

- (NSArray <SASalesOrderViewListModel *>*)getProductSkuArrayByColorPriceWithProduct:(SASalesOrderViewListModel*)product sizeArray:(NSArray *)sizeArray {
    
    NSMutableArray <SASalesOrderViewListModel *>*array = [NSMutableArray array];
    
    NSArray *colourNameArray = [product.toPendSkuAttrs valueForKeyPath:@"@distinctUnionOfObjects.colourName"];// 多少种颜色
    for (NSString *colourName in colourNameArray) {
        NSArray *filterArray = [product.toPendSkuAttrs filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
           SASalesOrderToPendSkuList *model = evaluatedObject;
            return [model.colourName isEqualToString:colourName];
        }]];
        NSArray *priceArray = [filterArray valueForKeyPath:@"@distinctUnionOfObjects.price"];// 多少种单价
        for (NSString *price in priceArray) {
            NSArray *filterArray2 = [filterArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
               SASalesOrderToPendSkuList *model = evaluatedObject;
                return [model.price isEqualToString:price];
            }]];
            
            if (filterArray2.count > 0) {
                product.toPendSkuAttrs = [NSMutableArray arrayWithArray:filterArray2];
                [array addObject:product];
            }
        }
    }
    
    return array;
}

@end
